package com.img2sgf;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestGalleryActivity extends Activity {

    //YOU CAN EDIT THIS TO WHATEVER YOU WANT
    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;
    //ADDED
    private String filemanagerstring;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("depues de super.onCreate(savedInstanceState);");
        setContentView(R.layout.main);
        System.out.println("despues de setContentView(R.layout.main);");
        
        ((Button) findViewById( R.id.boton1)).setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
            	System.out.println("comienza public void onClick(View arg0)");
                // in onCreate or any event where your want the user to
                // select a file
                Intent intent = new Intent();
                intent.setType( "image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                System.out.println("antes de  startActivityForResult");
                startActivityForResult(Intent.createChooser(intent,
                        "Select Picture"), SELECT_PICTURE);
                System.out.println("despues de startActivityForResult");
            }
        });
    }

    //UPDATED
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	System.out.println("comienza onActivityResult");
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();

                //OI FILE Manager
//                filemanagerstring = selectedImageUri.getPath();

                //MEDIA GALLERY
                selectedImagePath = getPath(selectedImageUri);

                //DEBUG PURPOSE - you can delete this if you want
//                if(selectedImagePath!=null)
//                    System.out.println(selectedImagePath);
//                else System.out.println("selectedImagePath is null");
//                if(filemanagerstring!=null)
//                    System.out.println(filemanagerstring);
//                else System.out.println("filemanagerstring is null");

                //NOW WE HAVE OUR WANTED STRING
//                if(selectedImagePath!=null)
//                    System.out.println("selectedImagePath is the right one for you!");
//                else
//                    System.out.println("filemanagerstring is the right one for you!");
                
//                Intent intent = new Intent();
//                intent.setDataAndType(selectedImageUri,  "image/*");
//                intent.setAction(Intent.ACTION_VIEW);
//                startActivityForResult(Intent.createChooser(intent,
//                        "View Picture"), VIEW_PICTURE);
                
                setContentView(new GobanView(this,selectedImageUri));
            }
        }
    }



	//UPDATED!
    public String getPath(Uri uri) {
    	System.out.println("comienza getPath");
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if(cursor!=null)
        {
            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            System.out.println("retorna " + cursor.getString(column_index));
            return cursor.getString(column_index);
        }
        else return null;
    }
}
